package com.ht.system.service.impl;

import java.rmi.ServerException;
import java.rmi.server.ServerCloneException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.validation.constraints.NotEmpty;

import cn.hutool.core.util.ObjectUtil;
import com.ht.common.core.constant.Constants;
import com.ht.common.core.constant.SysConstant;
import com.ht.common.core.enums.DeptTypeEnum;
import com.ht.common.core.exception.ServiceException;
import com.ht.common.core.utils.DateUtils;
import com.ht.common.core.utils.StringUtils;

import com.ht.common.security.utils.SecurityUtils;
import com.ht.system.domain.SysDept;
import com.ht.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ht.system.mapper.BaseFieldInfoMapper;
import com.ht.system.api.dto.DictDataDto;
import com.ht.system.api.dto.DictDto;
import com.ht.system.component.InitDictCommonComponent;
import com.ht.system.domain.BaseFieldInfo;
import com.ht.system.domain.Customs;
import com.ht.system.service.IBaseFieldInfoService;

/**
 * 场所信息Service业务层处理
 *
 * @author zmx
 * @date 2025-03-22
 */
@Service
public class BaseFieldInfoServiceImpl implements IBaseFieldInfoService
{
    @Autowired
    private BaseFieldInfoMapper baseFieldInfoMapper;
    @Autowired
    private InitDictCommonComponent initDictCommonComponent;

    @Autowired
    private ISysDeptService sysDeptService;


    @PostConstruct
    public void customsToDict(){
    	List<BaseFieldInfo>  cusList=baseFieldInfoMapper.selectBaseFieldInfoList(new BaseFieldInfo());
    	if(StringUtils.isNotEmpty(cusList)){
    		DictDto dictDto=new DictDto();
    		dictDto.setDictType(Constants.ENTITY_TO_DICT_PRE+"basefieldinfo");
    		dictDto.setDictName("监管场所信息字典");
    		List<DictDataDto> dictDataList= cusList.stream().map(n->new DictDataDto(n.getFieldCode(),n.getFieldName(),"1")).collect(Collectors.toList());
    		dictDto.setDictDataList(dictDataList);
    		initDictCommonComponent.add(dictDto);
    	}
    }

    /**
     * 查询场所信息
     *
     * @param fieldCode 场所信息主键
     * @return 场所信息
     */
    @Override
    public BaseFieldInfo selectBaseFieldInfoByFieldCode(String fieldCode)
    {
        return baseFieldInfoMapper.selectBaseFieldInfoByFieldCode(fieldCode);
    }

    /**
     * 查询场所信息列表
     *
     * @param baseFieldInfo 场所信息
     * @return 场所信息
     */
    @Override
    public List<BaseFieldInfo> selectBaseFieldInfoList(BaseFieldInfo baseFieldInfo)
    {
        return baseFieldInfoMapper.selectBaseFieldInfoList(baseFieldInfo);
    }

    /**
     * 新增场所信息
     *
     * @param baseFieldInfo 场所信息
     * @return 结果
     */
    @Override
    public int insertBaseFieldInfo(BaseFieldInfo baseFieldInfo)
    {
        baseFieldInfo.setCreateTime(DateUtils.getNowDate());
        BaseFieldInfo bf = baseFieldInfoMapper.selectBaseFieldInfoByFieldCode(baseFieldInfo.getFieldCode());
        if(ObjectUtil.isNotEmpty(bf)){
            throw new ServiceException("场所编码"+baseFieldInfo.getFieldCode()+"已存在");
        }
        genDept(baseFieldInfo);
        return baseFieldInfoMapper.insertBaseFieldInfo(baseFieldInfo);
    }

    private void genDept(BaseFieldInfo bF) {
        SysDept sdp = sysDeptService.selectByDeptName(bF.getFieldName(), SysConstant.SYS_ORG_ROOT_ID);
        if (null != sdp) {
            sdp.setDeptName(bF.getFieldName());
            sdp.setDeptCode(bF.getFieldCode());
            sdp.setDeptType(DeptTypeEnum.HC.getCode());
            sysDeptService.updateDept(sdp);
        } else {
            SysDept sd = new SysDept();
            sd.setDeptCode(bF.getFieldCode());
            sd.setDeptType(DeptTypeEnum.HC.getCode());
            sd.setAgentCode(bF.getFieldCode());
            sd.setCreateBy(SecurityUtils.getLoginUser().getNickname());
            sd.setCreateTime(new Date());
            sd.setDeptName(bF.getFieldName());
            sd.setParentId(SysConstant.SYS_ORG_ROOT_ID);
            sd.setParentCustomsCode(bF.getCustomsCode());
            sd.setStatus("0");
            sysDeptService.insertDept(sd);
        }
    }

    /**
     * 修改场所信息
     *
     * @param baseFieldInfo 场所信息
     * @return 结果
     */
    @Override
    public int updateBaseFieldInfo(BaseFieldInfo baseFieldInfo)
    {
        genDept(baseFieldInfo);
        baseFieldInfo.setUpdateTime(DateUtils.getNowDate());
        return baseFieldInfoMapper.updateBaseFieldInfo(baseFieldInfo);
    }

    /**
     * 批量删除场所信息
     *
     * @param fieldCodes 需要删除的场所信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseFieldInfoByFieldCodes(@NotEmpty(message="参数不能为空") String[] fieldCodes)
    {

    	int delRes=0;
    	for (String id : fieldCodes) {
    		delRes+=deleteBaseFieldInfoByFieldCode(id);
		}
        return delRes;
    }

    /**
     * 删除场所信息信息
     *
     * @param fieldCode 场所信息主键
     * @return 结果
     */
    public int deleteBaseFieldInfoByFieldCode(String fieldCode)
    {
        return baseFieldInfoMapper.deleteBaseFieldInfoByFieldCode(fieldCode);
    }

    @Override
    public List<BaseFieldInfo> selectByCustomsCode(String customsCode) {
        return baseFieldInfoMapper.selectByCustomsCode(customsCode);
    }
}
